<?php
namespace App\Http\Controllers;

use App\Http\Logic\ChainLogic;
use App\Http\Logic\TickersLogic;
use App\Http\Requests\Tickers\AddWithdrawOrderRequest;
use App\Http\Requests\Tickers\GetAllCoinsRequest;
use App\Http\Requests\Tickers\GetCandlesRequest;
use App\Http\Requests\Tickers\GetCoinChainsRequest;
use App\Http\Requests\Tickers\GetListsRequest;
use App\Http\Requests\Tickers\GetRechargeInfoRequest;
use Illuminate\Http\JsonResponse;

class TickersController extends BaseController
{
    public function getTabs()
    {
        $arr = [
            [
                "name" => __('coin-type.Main'),
                "sort" => "main"
            ],
            [
                "name" => __('coin-type.Hot'),
                "sort" => "heat"
            ],
            [
                "name" => __('coin-type.Increase'),
                "sort" => "inc"
            ],
            [
                "name" => __('coin-type.New'),
                "sort" => "ctime"
            ]
        ];

        return $this->success($arr);
    }

    /**
     * @notes  获取链列表
     * @param \App\Http\Requests\Tickers\GetListsRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/16 17:43
     */
    public function getLists(GetListsRequest $request) : JsonResponse
    {
        $sort = $request->input('sort');

        $pageNo = $request->input('pageNo', 1);

        $pageSize = $request->input('pageSize', 15);

        $search = $request->input('search') ?? '';

        switch ($sort) {
            case 'all':
                $list = TickersLogic::getAllChains($search, $pageNo, $pageSize);
                break;
            case 'main':
                $list = TickersLogic::getMainChains($search, $pageNo, $pageSize);
                break;
            case 'ctime':
                $list = TickersLogic::getChainsByCreateTime($search, $pageNo, $pageSize);
                break;
            case 'heat':
                $list = TickersLogic::getChainsByHeat($search, $pageNo, $pageSize);
                break;
            case 'inc':
                $list = TickersLogic::getChainsByInc($search, $pageNo, $pageSize);
                break;
            default:
                $list = [];
                break;
        }

        return $this->success($list);
    }

    /**
     * @notes  获取链行情K线数据
     * @param GetCandlesRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/17 10:52
     */
    public function getCandles(GetCandlesRequest $request) : JsonResponse
    {
        $inst_id = $request->input('instId');

        $bar = $request->input('bar');

        $after = $request->input('after','');

        $limit = $request->input('limit', 300);

        $list = TickersLogic::getMarketCandles($inst_id, $bar, $after, $limit);

        if(false === $list) {
            return $this->error(TickersLogic::getErrorMessage());
        }

        return $this->success($list);
    }

    /**
     * @notes 获取SWT行情信息
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/17 15:07
     */
    public function getSwtChains()
    {
        $data = TickersLogic::getSwtChains();

        return $this->success($data);
    }

    /**
     * @notes 获取所有可用币
     * @param GetAllCoinsRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/21 17:47
     */
    public function getAllCoins(GetAllCoinsRequest $request) : JsonResponse
    {
        $search = $request -> input('search') ?? '';

        $list = ChainLogic::getAllCoins($search);

        return $this->success($list);
    }

    /**
     * @notes 获取指定币的可交易网络
     * @param GetCoinChainsRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/22 14:08
     */
    public function getCoinChains(GetCoinChainsRequest $request) : JsonResponse
    {
        $coin_id = $request -> input('coin_id');

        $list = ChainLogic::getCoinChains($coin_id);

        return $this->success($list);
    }

    /**
     * @notes 获取充值页信息
     * @param GetRechargeInfoRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/22 16:05
     */
    public function getRechargeInfo(GetRechargeInfoRequest $request) : JsonResponse
    {
        $coin_id = $request -> input('coin_id');

        $network_id = $request -> input('network_id');

        $user_id = auth() -> user() -> id;

        $res = ChainLogic::getRechargeInfo($user_id, $coin_id, $network_id);

        if(false === $res) {
            return $this->error(ChainLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 获取已有货币可提币
     * @param GetAllCoinsRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/23 09:17
     */
    public function getUserCoins(GetAllCoinsRequest $request) : JsonResponse
    {
        $search = $request -> input('search') ?? '';

        $user_id = auth() -> user() -> id;

        $list = ChainLogic::getUserCoins($user_id, $search);

        return $this->success($list);
    }

    /**
     * @notes 增加提币订单
     * @param AddWithdrawOrderRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/23 11:23
     */
    public function addWithdrawOrder(AddWithdrawOrderRequest $request) : JsonResponse
    {
        $coin_id = $request -> input("coin_id");
        $network_id = $request -> input("network_id");
        $to_address = $request -> input("to_address");
        $num = $request -> input("num");
        $user_id = auth() -> user() -> id;

        $res = TickersLogic::addWithdrawOrder($coin_id, $network_id, $to_address, $num, $user_id);

        if(false === $res) {
            return $this->error(TickersLogic::getErrorMessage());
        }

        return $this->success();
    }

}
